package com.weishui.dimensionalbarcode.listener;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.text.BadLocationException;
import javax.swing.text.StyledDocument;

import com.google.zxing.Result;
import com.weishui.dimensionalbarcode.app.AppWindoow;
import com.weishui.dimensionalbarcode.logic.DimensionalBarEncodeAndDecode;

public class ButtonActionListener implements ActionListener {

	private AppWindoow appWindoow;
	public ButtonActionListener(AppWindoow appWindoow){
		this.appWindoow = appWindoow;
	}
	
	private static File imageFile;
	private static File selectedFile;
	private ImageIcon imageIcon;
	private StyledDocument doc;
	@Override
	public void actionPerformed(ActionEvent event) {
		// TODO Auto-generated method stub
		if("encode".equals(event.getActionCommand())){
			String content = appWindoow.textAreaContent.getText();
			if(content == null || "".equals(content)){
				JOptionPane.showMessageDialog(null, "请输入需要编码的内容!", "ERROR_MESSAGE",JOptionPane.ERROR_MESSAGE);
				return;
			}else{
				doc = appWindoow.textPaneImage.getStyledDocument();
				try {
					doc.remove(0, doc.getLength());
				} catch (BadLocationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				imageFile = DimensionalBarEncodeAndDecode.encode(content);
				if(imageFile == null){
					JOptionPane.showMessageDialog(null, "编码错误，无法生成文件!", "ERROR_MESSAGE",JOptionPane.ERROR_MESSAGE);
					return;
				}
				//获取二维码图片信息并设置在预览框中
				imageIcon = new ImageIcon(imageFile.getPath());
				appWindoow.textPaneImage.insertIcon(imageIcon);
			}
		}else if("save".equals(event.getActionCommand())){
			if(imageFile == null){
				JOptionPane.showMessageDialog(null, "没有文件，无法保存!", "ERROR_MESSAGE",JOptionPane.ERROR_MESSAGE);
				return;
			}
			JFileChooser chooser = new JFileChooser();
			chooser.setCurrentDirectory(new File("c:\\"));
			chooser.setFileSelectionMode(JFileChooser.SAVE_DIALOG);
			chooser.setDialogTitle("选择保存路径");
			int ret = chooser.showSaveDialog(new JDialog());
			if (ret == JFileChooser.APPROVE_OPTION) {
				File chooseFile = chooser.getSelectedFile();
				File newFile = new File(chooseFile.getPath() + "\\" + imageFile.getName());
				//移动二维码文件
				imageFile.renameTo(newFile);
				JOptionPane.showMessageDialog(null, "保存成功!", "保存",JOptionPane.INFORMATION_MESSAGE);
				doc = appWindoow.textPaneImage.getStyledDocument();
				try {
					doc.remove(0, doc.getLength());
				} catch (BadLocationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}else if("select".equals(event.getActionCommand())){
			JFileChooser chooser = new JFileChooser();
			chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
			FileNameExtensionFilter filter = new FileNameExtensionFilter("二维码文件(.png.jpg)", "png","jpg");
			chooser.setFileFilter(filter);
			chooser.setDialogTitle("选择二维码文件");
			// 文件选择框
			chooser.setCurrentDirectory(new File(System.getProperty("user.dir")));
			int ret = chooser.showOpenDialog(new JDialog());
			if (ret == JFileChooser.APPROVE_OPTION) {
				selectedFile = chooser.getSelectedFile();
				//清除上一次设置的图片
				doc = appWindoow.textPaneSelectImage.getStyledDocument();
				try {
					doc.remove(0, doc.getLength());
				} catch (BadLocationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				appWindoow.textPaneSelectImage.insertIcon(new ImageIcon(selectedFile.getPath()));
			}
		}else if ("decode".equals(event.getActionCommand())) {
			if(selectedFile == null ){
				JOptionPane.showMessageDialog(null, "请选择要解码的文件!", "ERROR_MESSAGE",JOptionPane.ERROR_MESSAGE);
				return;
			}
			//解码结果
			Result result = DimensionalBarEncodeAndDecode.decode(selectedFile);
			appWindoow.textAreaShowContent.setText(result.getText());
		}
	}

}
